package compendiumAid.presentation.options
{
	import compendiumAid.application.preferences.PreferencesEvent;
	import compendiumAid.domain.ApplicationCursorManager;
	import compendiumAid.domain.model.ApplicationModel;
	import compendiumAid.presentation.PresentationModel;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Size;
	import org.alivepdf.layout.Unit;
	
	[ManagedEvents(names="PreferencesEvent")]
	public class OptionsPM extends PresentationModel
	{
		[Inject]
		[Bindable]
		public var model:ApplicationModel;
		
		[Bindable]
		public var rememberPass:Boolean;
		
		[Bindable]
		public var autoLogin:Boolean;
		
		[Bindable]
		public var username:String;
		
		[Bindable]
		public var password:String;
		
		[Bindable]
		public var elementsPerRow:Number;
		
		[Bindable]
		public var elementOrientation:String;
		
		[Bindable]
		public var paperType:Size;
		
		[Bindable]
		public var marginSize:Number;
		
		[Bindable]
		public var unitType:String;
		
		[Bindable]
		public var orientationArray:ArrayCollection = new ArrayCollection
			([
				Orientation.LANDSCAPE,
				Orientation.PORTRAIT
			]);
		
		[Bindable]
		public var paperTypeArray:ArrayCollection = new ArrayCollection
			([
				Size.A3,
				Size.A4,
				Size.A5,
				Size.LEGAL,
				Size.LETTER,
				Size.TABLOID
			]);
		
		[Bindable]
		public var unitTypeArray:ArrayCollection = new ArrayCollection
			([
				Unit.INCHES,
				Unit.MM
			]);
		
		private var currentPopUp:UIComponent;
		
		public function showOptionsPopUp():void
		{
			if (currentPopUp == null)
			{
				currentPopUp = new OptionsPopUp;
				(currentPopUp as OptionsPopUp).pm = this;
				
				PopUpManager.addPopUp( currentPopUp, FlexGlobals.topLevelApplication.root, true );
				PopUpManager.centerPopUp( currentPopUp );
			}
		}
		
		public function showLoginPopUp():void
		{
			if(currentPopUp == null)
			{
				currentPopUp = new LoginPopUp;
				(currentPopUp as LoginPopUp).pm = this;
				
				PopUpManager.addPopUp( currentPopUp, FlexGlobals.topLevelApplication.root, true );
				PopUpManager.centerPopUp( currentPopUp );
				
				ApplicationCursorManager.hideBusyCursor = true;
			}
		}
		
		public function save():void
		{
			var preferencesObject:Object = new Object;
			
			preferencesObject.username = username;
			preferencesObject.password = password
			preferencesObject.rememberPass = rememberPass;
			preferencesObject.autoLogin = autoLogin;
			preferencesObject.elementsPerRow = elementsPerRow;
			preferencesObject.elementOrientation = elementOrientation;
			preferencesObject.paperType = paperType;
			preferencesObject.marginSize = marginSize;
			preferencesObject.unitType = unitType;
			
			dispatcher(new PreferencesEvent(PreferencesEvent.SAVE, preferencesObject));
			
			close();
		}
		
		public function load():void
		{
			var preferencesObject:Object = new Object;
			
			dispatcher(new PreferencesEvent(PreferencesEvent.LOAD, preferencesObject));
		}
		
		public function close():void
		{
			if (currentPopUp != null)
			{
				PopUpManager.removePopUp(currentPopUp);
				currentPopUp = null;
				
				ApplicationCursorManager.hideBusyCursor = false;
			}
		}
	}
}